package Views;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import BusinessObjects.ServerInfo;
import Models.ServerManagementModel;

import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class ServerManagementView extends JFrame implements Observer {
	private JLabel alert;
	private ServerManagementModel model;
	private JTextField textFieldPortTCPIP;
	private JTextField textFieldPortUDP;
	
	public ServerManagementView(ServerManagementModel model, ActionListener controller, String name) {
		this.model = model;
		model.addObserver(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 350);
		JPanel contentPane = new JPanel();
		contentPane.setBackground(new Color(204, 204, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		contentPane.setLayout(null);
		setTitle(name);
		
		JButton buttonDemarrer = new JButton("D\u00E9marrer");
		buttonDemarrer.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonDemarrer.setBounds(170, 144, 109, 22);
		contentPane.add(buttonDemarrer);	
		buttonDemarrer.addActionListener(controller);
		buttonDemarrer.setActionCommand("initServer");
			
		setContentPane(contentPane);
		
		JLabel lblMotDePasse = new JLabel("Port TCP/IP :");
		lblMotDePasse.setBounds(83, 116, 77, 14);
		contentPane.add(lblMotDePasse);
		
		JLabel lblLogin = new JLabel("Initialisation du serveur");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLogin.setBounds(83, 36, 306, 22);
		contentPane.add(lblLogin);
		
		textFieldPortTCPIP = new JTextField();
		textFieldPortTCPIP.setBounds(170, 113, 58, 20);
		contentPane.add(textFieldPortTCPIP);
		textFieldPortTCPIP.setColumns(4);
		
		alert = new JLabel("");
		alert.setForeground(Color.RED);
		alert.setBounds(36, 177, 338, 29);
		contentPane.add(alert);
		
		textFieldPortUDP = new JTextField();
		textFieldPortUDP.setColumns(4);
		textFieldPortUDP.setBounds(170, 85, 58, 20);
		contentPane.add(textFieldPortUDP);
		
		JLabel lblPortUdp = new JLabel("Port UDP :");
		lblPortUdp.setBounds(83, 88, 77, 14);
		contentPane.add(lblPortUdp);
	
		setVisible(true);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		alert.setText(model.getAlert());
	}
	
	public ServerInfo getServerInfos() {
		return new ServerInfo(0,  textFieldPortTCPIP.getText(), textFieldPortUDP.getText());
	}
}
